﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Storage;

namespace BenevolentSunData
{
    public class BeamLight
    {

        #region Light Properties
        Vector3 startPosition;
        public Vector3 StartPosition
        {
            get { return startPosition; }
            set {
                startPosition = value;
                //structStartPosition.SetValue(startPosition);
                Invalidate();
            }
        }

        Vector3 endPosition;
        public Vector3 EndPosition
        {
            get { return endPosition; }
            set {
                endPosition = value;
                //structEndPosition.SetValue(endPosition);
                Invalidate();
            }
        }

        Color color;
        public Color Color
        {
            get { return color; }
            set {
                color = value;
                //structColor.SetValue(color.ToVector3());
                Invalidate();
            }
        }

        float range;
        public float Range
        {
            get { return range; }
            set { 
                range = value;
                //structRange.SetValue(range);
                Invalidate();
            }
        }

        //float specularIntensity;
        //public float SpecularIntensity
        //{
            //get { return specularIntensity; }
           // set
            //{
                //specularIntensity = value;
            //}
        //}

        float fallOff;
        public float FallOff
        {
            get { return fallOff; }
            set {
                fallOff = value;
                //structFallOff.SetValue(fallOff);
                Invalidate();
            }
        }
        #endregion


        #region Effect
        Effect effect;

        EffectParameter structInstance;

        EffectParameter structStartPosition;

        EffectParameter structEndPosition;

        EffectParameter structColor;

        EffectParameter structRange;

        EffectParameter structFallOff;

        //EffectParameter structSpecularIntensity;

        int lightIndex = 0;
        #endregion


        #region Initialization
        public BeamLight(Effect effect)
        {
            this.effect = effect;
            /*
            structInstance = effect.Parameters["BeamLights"].Elements[0];
            structStartPosition = structInstance.StructureMembers["StartPosition"];
            structEndPosition = structInstance.StructureMembers["EndPosition"];
            structColor = structInstance.StructureMembers["Color"];
            structRange = structInstance.StructureMembers["Range"];
            structFallOff = structInstance.StructureMembers["FallOff"];
            //structSpecularIntensity = structInstance.StructureMembers["SpecularIntensity"];
            */
            StartPosition = Vector3.Zero;
            EndPosition = Vector3.One;
            Range = 30f;
            FallOff = 2f;
            Color = Color.White;
            //SpecularIntensity = 0.1f;
        }
        #endregion

        /*
        public void UpdateLight()
        {
            structColor.SetValue(color.ToVector3());
            structFallOff.SetValue(fallOff);
            structStartPosition.SetValue(startPosition);
            structEndPosition.SetValue(endPosition);
            structRange.SetValue(range);
            //structSpecularIntensity.SetValue(specularIntensity);
        }
        */
        public void UpdateLight(int lightIndex)
        {
            if (this.lightIndex == lightIndex)
            {
                return;
            }
            this.lightIndex = lightIndex;

            structInstance = effect.Parameters["BeamLights"].Elements[lightIndex];
            structStartPosition = structInstance.StructureMembers["StartPosition"];
            structEndPosition = structInstance.StructureMembers["EndPosition"];
            structColor = structInstance.StructureMembers["Color"];
            structRange = structInstance.StructureMembers["Range"];
            structFallOff = structInstance.StructureMembers["FallOff"];

            structColor.SetValue(color.ToVector3());
            structFallOff.SetValue(fallOff);
            structStartPosition.SetValue(startPosition);
            structEndPosition.SetValue(endPosition);
            structRange.SetValue(range);
        }

        public void Invalidate()
        {
            lightIndex = -1;
        }

    }
}
